import setting from './setting';

const LayoutsModel = {
  namespace: 'layoutApi',
  state: {
    openKeys: [], // 展开的菜单
    allMenuMap: [], // 所有一级菜单path
    iframeUrl: '', // iframe url
    tabsArr: [], // 菜单标签栏
    activeKey: '', // 当前激活 tab 面板的 key
    selectedKeys: [], // 当前激活菜单的key
    menuClass: [], // 整合最末级菜单的一维数组
  },
  effects: {},
  reducers: {
    // 点击修改标签激活的key
    setTabsActiveKey(state, { payload }) {
      return { ...state, activeKey: payload };
    },
    // 点击菜单添加到头部的标签栏中
    setPushHeaderTabs(state, { payload }) {
      return { ...state, tabsArr: payload };
    },
    // 更新iframe的url
    setIframeUrl(state, { target }) {
      const newUrl = state.menuClass.find((f) => f.key == target);
      return { ...state, iframeUrl: newUrl.url || '' };
    },
    // 初始化所有菜单path
    setAllMenuMap(state, { payload }) {
      return { ...state, allMenuMap: payload };
    },
    // 动态展开菜单
    setOpenKeys(state, { payload }) {
      return { ...state, openKeys: payload };
    },
    // 通过便签栏中已有的菜单定位左侧的菜单
    setLeftShowMenuKeys(state, { payload }) {
      const FK = state.tabsArr.find((f) => f.key === payload);
      return {
        ...state,
        openKeys: FK.pro_layout_parentKeys || [setting.state.defaultMenuTab],
        selectedKeys: FK?.key ? FK.key : [setting.state.defaultMenuTab],
      };
    },
    // 设置当前选中的菜单项 key 数组
    setMenuSelectKeys(state, { payload }) {
      return { ...state, selectedKeys: payload };
    },
    // 整合最末级菜单的一维数组
    setMenuClassTransform(state, { payload }) {
      return { ...state, menuClass: payload };
    },
    // 判断标签栏中是否已存在并更新
    isSetTabsData(state, { record }) {
      const tabObj = Object.assign([], state.tabsArr);
      const tabMap = tabObj.map((m) => m.key);
      let objRecord = record.path ? record : state.menuClass.find((f) => f.key === record.key);
      let newTabsArr = [];
      if (tabMap.indexOf(objRecord.key) == -1) {
        newTabsArr = tabObj.concat(objRecord);
      } else {
        newTabsArr = tabObj.map((m) => {
          // m.path == setting.defaultMenuTab
          //   ? (m.pro_layout_parentKeys = objRecord.pro_layout_parentKeys)
          //   : m;
          m.pro_layout_parentKeys = objRecord.pro_layout_parentKeys;
          return m;
        });
      }
      return { ...state, tabsArr: newTabsArr };
    },
    // 关闭tab标签
    closeTabs(state, { target }) {
      const tiveIndex = state.tabsArr.findIndex((f) => f.path == state.activeKey);
      let data = [];
      switch (target) {
        case 'all':
          data = state.tabsArr.filter((f) => f.closable == false);
          break;
        case 'right':
          data = state.tabsArr.filter((_, i) => i <= tiveIndex);
          break;
        case 'left':
          data = state.tabsArr.filter((f, i) => f.closable == false || i >= tiveIndex);
          break;
        case 'other':
          data = state.tabsArr.filter((f, i) => f.closable == false || i == tiveIndex);
          break;
      }
      return {
        ...state,
        tabsArr: data,
        activeKey: data[data.length - 1].key,
        selectedKeys: [data[data.length - 1].key],
        openKeys: data[data.length - 1]?.pro_layout_parentKeys,
      };
    },
  },
};
export default LayoutsModel;
